﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLiteDAL
{
    public class Links : BaseDAL, ILinks
    {
        public int Insert(Models.Links model)
        {
            if (model.ID <= 0)
                model.ID = Db.CreateKey("Links");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8),
					new SQLiteParameter("@Url", DbType.String,200),
					new SQLiteParameter("@Src", DbType.String,300),
					new SQLiteParameter("@HasLogo", DbType.Boolean,1),
					new SQLiteParameter("@ClassID", DbType.Int32,8),
					new SQLiteParameter("@Rank", DbType.Int32,8),
                    new SQLiteParameter("@CreateTime", DbType.DateTime),
					new SQLiteParameter("@Title", DbType.String,200)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Url;
            parameters[2].Value = model.Src;
            parameters[3].Value = model.HasLogo;
            parameters[4].Value = model.ClassID;
            parameters[5].Value = model.Rank;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.Title;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Links([ID],[Url],[Src],[HasLogo],[ClassID],[Rank],[CreateTime],[Title]) values (@ID,@Url,@Src,@HasLogo,@ClassID,@Rank,@CreateTime,@Title)", parameters);
        }

        public int Update(Models.Links model)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Url", DbType.String,200),
					new SQLiteParameter("@Src", DbType.String,300),
					new SQLiteParameter("@HasLogo", DbType.Boolean,1),
					new SQLiteParameter("@ClassID", DbType.Int32,8),
					new SQLiteParameter("@Rank", DbType.Int32,8),
                    new SQLiteParameter("@Title", DbType.String,200),
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.Url;
            parameters[1].Value = model.Src;
            parameters[2].Value = model.HasLogo;
            parameters[3].Value = model.ClassID;
            parameters[4].Value = model.Rank;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.ID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "update JNCms_Links set [Url]=@Url,[Src]=@Src,[HasLogo]=@HasLogo,[ClassID]=@ClassID,[Rank]=@Rank,[Title]=@Title where [ID]=@ID", parameters);
        }

        public int Delete(int id)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = id;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "delete from JNCms_Links where [ID]=@ID", parameters);
        }

        public List<Models.Links> GetList(int id)
        {
            List<Models.Links> list = new List<Models.Links>();
            SQLiteParameter parameters = new SQLiteParameter("@ClassID", DbType.Int32, 4);
            parameters.Value = id;

            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, string.Concat("select [ID],[Url],[Src],[HasLogo],[ClassID],[Rank],[CreateTime],[Title] from [JNCms_Links] where [ClassID]=@ClassID order by [Rank] desc"), parameters))
            {
                while (rd.Read())
                {
                    list.Add(Db.Entity.PopulateEntity<Models.Links>(rd));
                }
            }
            return list;
        }

        public List<Models.Links> GetList()
        {
            List<Models.Links> list = new List<Models.Links>();
            using (IDataReader rd = Db.Helper.ExecuteReader("select [ID],[Url],[Src],[HasLogo],[ClassID],[Rank],[CreateTime],[Title] from [JNCms_Links]  order by [Rank] desc"))
            {
                while (rd.Read())
                {
                    list.Add(Db.Entity.PopulateEntity<Models.Links>(rd));
                }
            }
            return list;
        }

        public Models.Links GetItem(int item)
        {
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from [JNCms_Links] where [ID]=@ID limit 1", new SQLiteParameter("@ID", item)))
            {
                if (rd.Read())
                {
                    return Db.Entity.PopulateEntity<Models.Links>(rd);
                }
            }
            return null;
        }
    }
}
